# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  def home_url
    url_for :controller => 'schools', :action => 'index'
  end
  
  def show_datetime(time)
    time += 8.hour # set timezone to utc, so +0800 for bj timezone
    "#{time.year}-#{time.month}-#{time.day} #{time.hour}:#{time.min}:#{time.sec}"
  end
  
  def show_date(time)
    time += 8.hour
    "#{time.year}-#{time.month}-#{time.day}"
  end
  
  def error_explan(object, error, msg)
    return "<span class=\"errorExplan\">#{msg}</span>" if object && object.errors[error]
  end
  
  def avatar_for(user, size='medium')
    user.avatar_url ? image_tag(url_for_file_column(user, "avatar_url", size)) : image_tag("/images/avatar_#{size}.png")
  end
  
  def chinese_select_date(res, col)
    year_select = "<select id=\"#{res}_#{col}_1i\" name=\"#{res}[#{col}(1i)]\">"
  
    2003.upto(2013) do |year|
      year_select += ("<option value=\"#{year}\" " + (year == Time.now.year ? "selected=\"selected\"" : "") + ">#{year}</option>")
    end
    year_select += "</select>"
 
    month_select = "<select id=\"#{res}_#{col}_2i\" name=\"#{res}[#{col}(2i)]\">"
    %w(一月 二月 三月 四月 五月 六月 七月 八月 九月 十月 十一月 十二月).each_with_index do |month, i|
      month_select += ("<option value=\"#{mon=i+1}\"" + (mon == Time.now.month ? "selected=\"selected\"" : "") + ">#{month}</option>")
    end
    month_select += "</select>"
    
    day_select = "<select id=\"#{res}_#{col}_3i\" name=\"#{res}[#{col}(3i)]\">"
    1.upto(31) do |day|
      day_select += ("<option value=\"#{day}\"" + (day == Time.now.day ? "selected=\"selected\"" : "") + ">#{day}</option>")
    end
    day_select += "</select>"
    
    return year_select+month_select+day_select
  end
end
